﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using KerriganDirection.SessionMgt;
using KerriganDirection.VehicleMgt;
using KerriganDirection.InstructorMgt;
using System.Diagnostics;
using System.Windows.Controls;
using KerriganDirection.Helpers;
using KerriganDirection.DirectionServiceWCF;

namespace KerriganDirection
{
    public class MainModule : INotifyPropertyChanged
    {
        #region NotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        #endregion

        #region Properties

        private SessionModule _SessionModuleVO;
        public SessionModule SessionModuleVO
        {
            get
            {
                return this._SessionModuleVO;
            }
            set
            {
                this._SessionModuleVO = value;
                OnPropertyChanged("SessionModuleVO");
            }
        }

        private VehicleModule _VehicleModuleVO;
        public VehicleModule VehicleModuleVO
        {
            get
            {
                return this._VehicleModuleVO;
            }
            set
            {
                this._VehicleModuleVO = value;
                OnPropertyChanged("VehicleModuleVO");
            }
        }

        private InstructorModule _InstructorModuleVO;
        public InstructorModule InstructorModuleVO
        {
            get
            {
                return this._InstructorModuleVO;
            }
            set
            {
                this._InstructorModuleVO = value;
                OnPropertyChanged("InstructorModuleVO");
            }
        }

        private MainWindow _WindowVO;
        public MainWindow WindowVO
        {
            get
            {
                return this._WindowVO;
            }
            set
            {
                this._WindowVO = value;
                OnPropertyChanged("WindowVO");
            }
        }

        #endregion

        #region Constructors

        public MainModule()
        {

        }

        #endregion

        #region Methods

        public void Init()
        {
            try
            {
                //Get Instance application/Main window
                this.WindowVO = (MainWindow)App.Current.Windows[0];

                //Initiatization Proxy
                ServiceProxy.InitService();

                //Initialization Modules
                this.SessionModuleVO = new SessionModule();
                this.VehicleModuleVO = new VehicleModule();
                this.InstructorModuleVO = new InstructorModule();

                this.SessionModuleVO.InitModule();
                this.VehicleModuleVO.InitModule();
                this.InstructorModuleVO.InitModule();

                //Add uc
                this.ChangeUC(this.WindowVO.SessionMgtTabItemSP, this.SessionModuleVO.UcSessionListVO);
                this.ChangeUC(this.WindowVO.InstructorMgtTabItemSP, this.InstructorModuleVO.UcInstructorListVO);
                this.ChangeUC(this.WindowVO.VehicleMgtTabItemSP, this.VehicleModuleVO.UcVehicleListVO);


                //Loading start page status on Session Management
                this.WindowVO.MainTabControl.SelectedIndex = 0;

                //Affichage button
                this.SessionModuleVO.UcSessionOptionsVO.bttAddSession.Visibility = System.Windows.Visibility.Visible;
                this.SessionModuleVO.UcSessionOptionsVO.bttCancelSession.Visibility = System.Windows.Visibility.Collapsed;
                this.SessionModuleVO.UcSessionOptionsVO.bttSaveSession.Visibility = System.Windows.Visibility.Collapsed;

                this.InstructorModuleVO.UcInstructorOptionsVO.bttAddAvailibility.Visibility = System.Windows.Visibility.Visible;
                this.InstructorModuleVO.UcInstructorOptionsVO.bttAddInstructor.Visibility = System.Windows.Visibility.Visible;
                this.InstructorModuleVO.UcInstructorOptionsVO.bttSaveInstructor.Visibility = System.Windows.Visibility.Collapsed;
                this.InstructorModuleVO.UcInstructorOptionsVO.bttSaveAvailability.Visibility = System.Windows.Visibility.Collapsed;
                this.InstructorModuleVO.UcInstructorOptionsVO.bttCancelInstructor.Visibility = System.Windows.Visibility.Collapsed;

                this.VehicleModuleVO.UcVehicleOptionsVO.bttAddVehicle.Visibility = System.Windows.Visibility.Visible;
                this.VehicleModuleVO.UcVehicleOptionsVO.bttCancelVehicle.Visibility = System.Windows.Visibility.Collapsed;
                this.VehicleModuleVO.UcVehicleOptionsVO.bttSaveVehicle.Visibility = System.Windows.Visibility.Collapsed;
            }
            catch (Exception ex)
            {
                throw new Exception("Error Initialization Service Proxy : " + ex.Message);
            } 
        }

        public void ChangeUC(StackPanel Parent, UserControl Child)
        {
            try
            {
                Parent.Children.Clear();
                Parent.Children.Add(Child);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        #endregion

        #region Navigation

        #region Session Navigation

        public void GoToAddSession()
        {
            this.SessionModuleVO.UcAddSessionVO = new ucAddSession();
            this.SessionModuleVO.UcSessionOptionsVO.bttAddSession.Visibility = System.Windows.Visibility.Collapsed;
            this.SessionModuleVO.UcSessionOptionsVO.bttCancelSession.Visibility = System.Windows.Visibility.Visible;
            this.SessionModuleVO.UcSessionOptionsVO.bttSaveSession.Visibility = System.Windows.Visibility.Visible;
            this.ChangeUC(this.WindowVO.SessionMgtTabItemSP, this.SessionModuleVO.UcAddSessionVO);
        }

        public void GoToLitSession()
        {
            this.SessionModuleVO.UcSessionListVO.RefreshList();
            this.SessionModuleVO.UcSessionOptionsVO.bttAddSession.Visibility = System.Windows.Visibility.Visible;
            this.SessionModuleVO.UcSessionOptionsVO.bttCancelSession.Visibility = System.Windows.Visibility.Collapsed;
            this.SessionModuleVO.UcSessionOptionsVO.bttSaveSession.Visibility = System.Windows.Visibility.Collapsed;
            this.ChangeUC(this.WindowVO.SessionMgtTabItemSP, this.SessionModuleVO.UcSessionListVO);
        }

        #endregion

        #region Instructor Navigation

        public void GoToEditInstructor()
        {
            this.InstructorModuleVO.UcInstructorOptionsVO.bttAddAvailibility.Visibility = System.Windows.Visibility.Collapsed;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttAddInstructor.Visibility = System.Windows.Visibility.Collapsed;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttSaveInstructor.Visibility = System.Windows.Visibility.Visible;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttSaveAvailability.Visibility = System.Windows.Visibility.Collapsed;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttCancelInstructor.Visibility = System.Windows.Visibility.Visible;
            this.ChangeUC(this.WindowVO.InstructorMgtTabItemSP, this.InstructorModuleVO.UcEditInstructorVO);
        }

        public void GoToAddInstructor()
        {
            this.InstructorModuleVO.UcAddInstructorVO = new ucAddInstructor();
            this.InstructorModuleVO.UcInstructorOptionsVO.bttAddAvailibility.Visibility = System.Windows.Visibility.Collapsed;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttAddInstructor.Visibility = System.Windows.Visibility.Collapsed;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttSaveInstructor.Visibility = System.Windows.Visibility.Visible;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttSaveAvailability.Visibility = System.Windows.Visibility.Collapsed;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttCancelInstructor.Visibility = System.Windows.Visibility.Visible;
            this.ChangeUC(this.WindowVO.InstructorMgtTabItemSP, this.InstructorModuleVO.UcAddInstructorVO);
        }

        public void GoToLitInstructor()
        {
            this.InstructorModuleVO.UcInstructorListVO.RefreshList();
            this.InstructorModuleVO.UcInstructorOptionsVO.bttAddAvailibility.Visibility = System.Windows.Visibility.Visible;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttAddInstructor.Visibility = System.Windows.Visibility.Visible;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttSaveInstructor.Visibility = System.Windows.Visibility.Collapsed;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttSaveAvailability.Visibility = System.Windows.Visibility.Collapsed;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttCancelInstructor.Visibility = System.Windows.Visibility.Collapsed;
            this.ChangeUC(this.WindowVO.InstructorMgtTabItemSP, this.InstructorModuleVO.UcInstructorListVO);
        }

        public void GoToAddInstructorAvailability()
        {
            this.InstructorModuleVO.UcInstructorOptionsVO.bttAddAvailibility.Visibility = System.Windows.Visibility.Collapsed;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttAddInstructor.Visibility = System.Windows.Visibility.Collapsed;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttSaveInstructor.Visibility = System.Windows.Visibility.Collapsed;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttSaveAvailability.Visibility = System.Windows.Visibility.Visible;
            this.InstructorModuleVO.UcInstructorOptionsVO.bttCancelInstructor.Visibility = System.Windows.Visibility.Visible;
            this.InstructorModuleVO.UcAddInstructorAvailabilityVO = new ucAddInstructorAvailability();
            this.ChangeUC(this.WindowVO.InstructorMgtTabItemSP, this.InstructorModuleVO.UcAddInstructorAvailabilityVO);
        }

        #endregion

        #region Vehicle Navigation

        public void GoToEditVehicle()
        {
            this.VehicleModuleVO.UcVehicleOptionsVO.bttAddVehicle.Visibility = System.Windows.Visibility.Collapsed;
            this.VehicleModuleVO.UcVehicleOptionsVO.bttCancelVehicle.Visibility = System.Windows.Visibility.Visible;
            this.VehicleModuleVO.UcVehicleOptionsVO.bttSaveVehicle.Visibility = System.Windows.Visibility.Visible;
            this.ChangeUC(this.WindowVO.VehicleMgtTabItemSP, this.VehicleModuleVO.UcEditVehicleVO);
        }

        public void GoToAddVehicle()
        {
            this.VehicleModuleVO.UcAddVehicleVO = new ucAddVehicle();
            this.VehicleModuleVO.UcVehicleOptionsVO.bttAddVehicle.Visibility = System.Windows.Visibility.Collapsed;
            this.VehicleModuleVO.UcVehicleOptionsVO.bttCancelVehicle.Visibility = System.Windows.Visibility.Visible;
            this.VehicleModuleVO.UcVehicleOptionsVO.bttSaveVehicle.Visibility = System.Windows.Visibility.Visible;
            this.ChangeUC(this.WindowVO.VehicleMgtTabItemSP, this.VehicleModuleVO.UcAddVehicleVO);
        }

        public void GoToLitVehicle()
        {
            this.VehicleModuleVO.UcVehicleOptionsVO.bttAddVehicle.Visibility = System.Windows.Visibility.Visible;
            this.VehicleModuleVO.UcVehicleOptionsVO.bttCancelVehicle.Visibility = System.Windows.Visibility.Collapsed;
            this.VehicleModuleVO.UcVehicleOptionsVO.bttSaveVehicle.Visibility = System.Windows.Visibility.Collapsed;
            this.VehicleModuleVO.UcVehicleListVO.RefreshList();
            this.ChangeUC(this.WindowVO.VehicleMgtTabItemSP, this.VehicleModuleVO.UcVehicleListVO);
        }

        #endregion

        #endregion
    }
}
